home *** CD-ROM | disk | FTP | other *** search
- /* @(#)execvp.c 2.1 */
- /* 3.0 SID # 1.2 */
- /*LINTLIBRARY*/
- /*
- * auxexeclp(name, arg,...,0) (like auxexecl, but does path search)
- * auxexecvp(name, argv) (like auxexecv, but does path search)
- */
- #include <LibAUX.h>
- #include </:usr:include:limits.h>
- #include </:usr:include:varargs.h>
-
- #ifndef NULL
- #define NULL 0
- #endif
- #define MAXARGS 256
-
- static char *execat(), shell[] = "/bin/sh";
- extern char *auxgetenv(), *strchr();
- int strlen(), GetAUXErrno(), auxexecv();
- void SetAUXErrno();
-
- /*VARARGS1*/
- int
- auxexeclp(name,va_alist)
- char *name;
- va_dcl
- {
- va_list ap;
- char *args[MAXARGS];
- int count = 0;
-
- va_start(ap);
- while ((args[count++] = va_arg(ap, char *)) != (char *) NULL)
- continue;
- va_end(ap);
- return(auxexecvp(name, args));
- }
-
- int
- auxexecvp(name, argv)
- char *name, **argv;
- {
- char *pathstr;
- char fname[2 * PATH_MAX];
- char *newargs[MAXARGS];
- int i;
- register char *cp;
- register unsigned etxtbsy=1;
- register int eacces=0;
- long error;
-
- if (strlen(name) > PATH_MAX) {
- SetAUXErrno(ENAMETOOLONG);
- return(-1);
- }
-
- if((pathstr = auxgetenv("PATH")) == NULL)
- pathstr = ":/bin:/usr/bin";
- cp = strchr(name, '/')? "": pathstr;
-
- do {
- cp = execat(cp, name, fname);
- retry:
- (void) auxexecv(fname, argv);
- error = GetAUXErrno();
- switch(error) {
- case ENOEXEC:
- newargs[0] = "sh";
- newargs[1] = fname;
- for(i=1; newargs[i+1]=argv[i]; ++i) {
- if(i >= (MAXARGS - 2)) {
- SetAUXErrno(E2BIG);
- return(-1);
- }
- }
- (void) auxexecv(shell, newargs);
- return(-1);
- case ETXTBSY:
- return(-1);
- case EACCES:
- ++eacces;
- break;
- case ENOMEM:
- case E2BIG:
- return(-1);
- }
- } while(cp);
- if(eacces)
- SetAUXErrno(EACCES);
- return(-1);
- }
-
- static char *
- execat(s1, s2, si)
- register char *s1, *s2;
- char *si;
- {
- register char *s;
-
- s = si;
- while(*s1 && *s1 != ':')
- *s++ = *s1++;
- if(si != s)
- *s++ = '/';
- while(*s2)
- *s++ = *s2++;
- *s = '\0';
- return(*s1? ++s1: 0);
- }
-